package dal

import (
	"context"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	"gorm.io/gen"
	"gorm.io/gen/field"
	"mall_admin_api/biz/model"
)

type OrdersItemDAO struct {
	bizID int64
}

type OrdersItemListParams struct {
	Condition []gen.Condition
	Page      int
	Size      int
	Sort      []field.Expr
}

func newOrdersItemDAO(bizID int64) *OrdersItemDAO {
	return &OrdersItemDAO{
		bizID: bizID,
	}
}

func (c *OrdersItemDAO) MGetOrdersItem(ctx context.Context, ids []int64) (map[int64]*model.OrdersItem, error) {
	list, err := OrdersItem.WithContext(ctx).Where(
		OrdersItem.BizID.Eq(c.bizID),
		OrdersItem.ID.In(ids...),
		OrdersItem.DeletedAt.IsNull(),
	).Find()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.MGetOrdersItem err: %v", err)
		return nil, err
	}
	var data = make(map[int64]*model.OrdersItem)
	for _, info := range list {
		data[info.ID] = info
	}
	return data, nil
}

func (c *OrdersItemDAO) GetOrdersItemByConditions(ctx context.Context, condition []gen.Condition) (*model.OrdersItem, error) {
	tx := OrdersItem.WithContext(ctx).Where(
		OrdersItem.BizID.Eq(c.bizID),
		OrdersItem.DeletedAt.IsNull(),
	)
	if len(condition) > 0 {
		tx = tx.Where(condition...)
	}
	info, err := tx.First()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetOrdersItemByConditions failed, %s", err.Error())
		return nil, err
	}
	return info, nil
}

func (c *OrdersItemDAO) GetOrdersItemList(ctx context.Context, condition []gen.Condition, page, size int) ([]*model.OrdersItem, error) {
	tx := OrdersItem.WithContext(ctx).Where(OrdersItem.BizID.Eq(c.bizID), OrdersItem.DeletedAt.IsNull())
	if len(condition) > 0 {
		tx = tx.Where(condition...)
	}
	if page > 0 && size > 0 {
		tx = tx.Offset((page - 1) * size).Limit(size)
	}
	list, err := tx.Find()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetOrdersItemList err: %v", err)
		return nil, err
	}
	return list, nil
}

func (c *OrdersItemDAO) GetOrdersItemTotal(ctx context.Context, condition []gen.Condition) (int64, error) {
	total, err := OrdersItem.WithContext(ctx).Where(OrdersItem.BizID.Eq(c.bizID), OrdersItem.DeletedAt.IsNull()).Where(condition...).Count()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetOrdersItemTotal error: %v", err)
		return 0, err
	}
	return total, nil
}

func (c *OrdersItemDAO) GetOrdersItemInfoByID(ctx context.Context, id int64) (*model.OrdersItem, error) {
	info, err := OrdersItem.WithContext(ctx).Where(OrdersItem.BizID.Eq(c.bizID), OrdersItem.DeletedAt.IsNull()).Where(OrdersItem.ID.Eq(id)).First()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetOrdersItemInfoByID error: %v", err)
		return nil, err
	}
	return info, nil
}

func (c *OrdersItemDAO) GetListWithTotal(ctx context.Context, params *OrdersItemListParams) ([]*model.OrdersItem, int64, error) {
	tx := OrdersItem.WithContext(ctx).Where(OrdersItem.BizID.Eq(c.bizID), OrdersItem.DeletedAt.IsNull())
	if len(params.Condition) > 0 {
		tx = tx.Where(params.Condition...)
	}
	if len(params.Sort) > 0 {
		tx = tx.Order(params.Sort...)
	}
	list, total, err := tx.FindByPage((params.Page-1)*params.Size, params.Size)
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.OrdersItem.GetListWithTotal err: %v", err)
		return nil, 0, err
	}
	return list, total, nil
}

func (c *OrdersItemDAO) CreateOrdersItem(ctx context.Context, data *model.OrdersItem) (*model.OrdersItem, error) {
	if err := OrdersItem.WithContext(ctx).Create(data); err != nil {
		hlog.CtxErrorf(ctx, "dal.CreateOrdersItem error: %v", err)
		return nil, err
	}
	return data, nil
}

func (c *OrdersItemDAO) CreateOrdersItemWithTx(ctx context.Context, tx *Query, data *model.OrdersItem) (*model.OrdersItem, error) {
	if err := tx.OrdersItem.WithContext(ctx).Create(data); err != nil {
		hlog.CtxErrorf(ctx, "dal.CreateOrdersItem error: %v", err)
		return nil, err
	}
	return data, nil
}

func (c *OrdersItemDAO) BatchCreateOrdersItem(ctx context.Context, data []*model.OrdersItem) error {
	if err := OrdersItem.WithContext(ctx).CreateInBatches(data, 20); err != nil {
		hlog.CtxErrorf(ctx, "dal.CreateInBatchesOrdersItem error: %v", err)
		return err
	}
	return nil
}

func (c *OrdersItemDAO) BatchCreateOrdersItemWithTx(ctx context.Context, tx *Query, data []*model.OrdersItem) ([]*model.OrdersItem, error) {
	if err := tx.OrdersItem.WithContext(ctx).CreateInBatches(data, 20); err != nil {
		hlog.CtxErrorf(ctx, "dal.BatchCreateWithTxOrdersItem error: %v", err)
		return nil, err
	}
	return data, nil
}

func (c *OrdersItemDAO) UpdateOrdersItem(ctx context.Context, condition []gen.Condition, fields []field.AssignExpr) error {
	_, err := OrdersItem.WithContext(ctx).Where(condition...).UpdateSimple(fields...)
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.UpdateOrdersItem error: %v", err)
		return err
	}
	return nil
}

func (c *OrdersItemDAO) UpdateOrdersItemWithTx(ctx context.Context, tx *Query, condition []gen.Condition, fields []field.AssignExpr) error {
	_, err := tx.OrdersItem.WithContext(ctx).Where(condition...).UpdateSimple(fields...)
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.UpdateOrdersItem error: %v", err)
		return err
	}
	return nil
}

func (c *OrdersItemDAO) DeleteOrdersItem(ctx context.Context, condition []gen.Condition) error {
	_, err := OrdersItem.WithContext(ctx).Where(condition...).Delete()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.DeleteOrdersItem error: %v", err)
		return err
	}
	return nil
}

func (c *OrdersItemDAO) DeleteOrdersItemWithTx(ctx context.Context, tx *Query, condition []gen.Condition) error {
	_, err := tx.OrdersItem.WithContext(ctx).Where(condition...).Delete()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.DeleteOrdersItemWithTx error: %v", err)
		return err
	}
	return nil
}
